<template>
  <div class="dw">
    <!-- <img alt="Vue logo" src="../assets/logo.png"> -->
    <h1>关于高德地图定位问题</h1>
    <div class="auto-map">
      <p>自动获取定位：<span v-text="autolocation"></span></p>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import AMap from 'AMap' // 引入高德地图

export default {
  name: 'dw',
  components: {
    // HelloWorld
  },
  data() {
    return {
      autolocation: "北京",
    }
  },
  mounted() {
    this.getLocation();
  },
  methods: {
    // 获取定位
    getLocation() {
      const _this = this;
      AMap.plugin('AMap.CitySearch', function () {
        var citySearch = new AMap.CitySearch()
        citySearch.getLocalCity(function (status, result) {
          if (status === 'complete' && result.info === 'OK') {
            // 查询成功，result即为当前所在城市信息
           window.console.log('通过ip获取当前城市：',result.city);
           _this.$emit('dw',result.city)
            _this.autolocation = result.city;
          }
        })
      });
    }
  }
}
</script>
